export type CategoryTree = {
    id: number;
    parent: number;
    name: string;
    sort: number;
    children?: CategoryTree[];
};

class Tool {
    public static array2Tree(array: CategoryTree[], parentId: number): CategoryTree[] {
        if (!array || array.length == 0) {
            return [];
        }
        const result: CategoryTree[] = [];
        for (let i = 0; i < array.length; i++) {
            const c = array[i];
            if (Number(c.parent) === Number(parentId)) {
                result.push(c);
                const children = Tool.array2Tree(array, c.id);
                if (children && children.length > 0) {
                    c.children = children;
                }
            }
        }
        return result;
    }
}

export default Tool
